// miswo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "soapH.h"
#include "ServiceSoap.nsmap"

#define progname	"miswo.exe"
#define	version		"1.1.0"
#define DEBUG		1

void usage(void)
{
	fprintf(stderr, "Dump Work Order from Shopfloor(VS2010)   Version: %s\n"
					"\n(c) Tody 2016, T-ware Inc.\n"
					"\nUsage:\n\t%s -url http://172.16.80.161:6000/Service.asmx [-sn <Serialnumber>] | [-std]"
					"\nOptions:\n"
					"      -url <website>       website must be assigned\n"					
					"      -std                 dump standard ini file from mes server\n"
					"      -sn                  dump work order from MES\n", version, progname );

	exit(1);
}

int main(int argc, char *argv[])
{
	struct soap						*soap;
	struct _WO__GetWoInfo 			sn;
	struct _WO__GetWoInfoResponse 	retData;
	struct _WO__OutSection			OutSection;
	struct _WO__OutSectionResponse OutSectionRes;


	char MISDATA[4001];   /*Size is ok for later use?*/
	char *website;
	char *father;
	char realweb[512];
	
	memset(MISDATA, 0, sizeof(MISDATA));
	
	if (argc >= 3 && strcmp("-url" ,argv[1])!=0)
	{
		usage();
	}else if (argc >= 3 && strstr(argv[2], "http://") != NULL)
	{
		website = argv[2];
		memset(realweb, 0, 512);
		strcpy(realweb, argv[2]);
		website = realweb;
	}else{
		/*using default website.*/
		website = NULL;
	}

	if ( argc == 5 && strcmp("-sn", argv[3])==0 )
	{
		fprintf(stderr, "Dump Work Order from Shopfloor(VS2017)   Version: %s\nAccessing %s...\n",version, website==NULL? "Default website":website);

		soap = soap_new();

		sn.sn = argv[4];
		retData.GetWoInfoResult = MISDATA;

		if (soap_call___WO2__GetWoInfo(soap, website, NULL, &sn, &retData) == SOAP_OK)
		{
			if (strlen(retData.GetWoInfoResult) != 0)
			{
				fprintf(stdout, "\n-------------------------------------------------------------------------------"); 
				//fprintf(stdout, "\nMIS: %s", retData.GetWoInfoResult); 
				father= strtok(retData.GetWoInfoResult, "|");
				while (father!=NULL)
				{
					fprintf(stdout, "\n%s", father); 
					father = strtok(NULL, "|");
				}
				fprintf(stdout, "\n-------------------------------------------------------------------------------");
				fflush(stdout);
			}else{
				soap_end(soap);
				free(soap);
				printf(" -> data not found!");
				exit(1);
			}
		}
		else
		{
			fprintf(stdout, "[-] network issue!\n");
			soap_print_fault(soap, stderr);
			soap_destroy(soap);
			soap_end(soap);
			soap_done(soap);
			exit(1);
		}
	}else if (argc == 4 && strcmp("-std", argv[3])==0){
		soap = soap_new();
		OutSectionRes.OutSectionResult = MISDATA;
		
		if(soap_call___WO2__OutSection(soap, website, NULL, NULL, &OutSectionRes)== SOAP_OK){
			if (strlen(OutSectionRes.OutSectionResult) != 0)
			{
				//fprintf(stdout, "\n-------------------------------------------------------------------------------"); 
				//fprintf(stdout, "\nMIS: %s", OutSectionRes.OutSectionResult); 
				father= strtok(OutSectionRes.OutSectionResult, "|");
				while (father!=NULL)
				{
					if(strstr(father, "[") && strstr(father, "]"))
						fprintf(stdout, "\n\n%s", father);
					else
						fprintf(stdout, "\n%s", father);
					father = strtok(NULL, "|");
				}
				//fprintf(stdout, "\n-------------------------------------------------------------------------------");
				fflush(stdout);
			}
		}else{
			fprintf(stdout, "[-] network issue!\n");
			soap_print_fault(soap, stderr);
			soap_destroy(soap);
			soap_end(soap);
			soap_done(soap);
			exit(1);
		}
	}else{
		usage();
	}

	soap_destroy(soap);
	soap_end(soap);
	soap_done(soap);

	exit(0);  /*normal exit from program*/
}


